/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.alvin.swing.ui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenu;
import javax.swing.JTable;
import org.alvin.swing.ui.action.ActionManager;
import org.alvin.swing.ui.action.AddAction;
import org.alvin.swing.ui.action.DelAction;

/**
 *
 * @author Administrator
 */
public class ActionDemoFrame extends javax.swing.JFrame {

    //临时这么放，实际项目中需要放在公用类中
    public static final ActionDemoFrame frame = new ActionDemoFrame();

    /**
     * Creates new form ActionDemoFrame
     */
    private ActionDemoFrame() {
        initComponents();
        this.addBtn.setAction(ActionManager.getAction(AddAction.class));
        this.delBtn.setAction(ActionManager.getAction(DelAction.class));
        this.jTable1.getSelectionModel().addListSelectionListener(e -> {
            if (e.getValueIsAdjusting()) {
                return;
            }
            ActionManager.firePropertyChanged();
        });
        initMenu();
        initToolbar();
        initPopeMenu();
    }

    public JTable getTable() {
        return this.jTable1;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        addBtn = new javax.swing.JButton();
        delBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jMenuBar1 = new javax.swing.JMenuBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addBtn.setText("jButton1");
        jPanel1.add(addBtn);

        delBtn.setText("jButton2");
        jPanel1.add(delBtn);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jToolBar1.setRollover(true);
        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);
        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ActionDemoFrame.frame.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton delBtn;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    private void initMenu() {
        JMenu fileMenu = new JMenu("File");
        this.jMenuBar1.add(fileMenu);
        fileMenu.add(ActionManager.getAction(AddAction.class));
        fileMenu.addSeparator();
        fileMenu.add(ActionManager.getAction(DelAction.class));
    }

    private void initToolbar() {
        this.jToolBar1.add(ActionManager.getAction(AddAction.class));
        this.jToolBar1.addSeparator();
        this.jToolBar1.add(ActionManager.getAction(DelAction.class));
    }

    private void initPopeMenu() {
        jTable1.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
                    jPopupMenu1.show(jTable1, (int) e.getPoint().getX(), (int) e.getPoint().getY());
                }
            }

        });
        jPopupMenu1.add(ActionManager.getAction(AddAction.class));
        jPopupMenu1.addSeparator();
        jPopupMenu1.add(ActionManager.getAction(DelAction.class));
    }
}
